package cn.tealc.wutheringwavestool.ui;

import cn.tealc.wutheringwavestool.base.Config;
import cn.tealc.wutheringwavestool.base.NotificationKey;
import cn.tealc.wutheringwavestool.base.NotificationManager;
import cn.tealc.wutheringwavestool.dao.UserInfoDao;
import cn.tealc.wutheringwavestool.model.ResponseBody;
import cn.tealc.wutheringwavestool.model.release.Release;
import cn.tealc.wutheringwavestool.thread.CheckGameConfigTask;
import cn.tealc.wutheringwavestool.thread.GameLogFileAnalysisTask;
import com.kuro.kujiequ.model.sign.UserInfo;
import com.kuro.kujiequ.model.towerData.DifficultyTotal;
import cn.tealc.wutheringwavestool.model.message.MessageInfo;
import cn.tealc.wutheringwavestool.model.message.MessageType;
import cn.tealc.wutheringwavestool.thread.CheckVersionTask;
import com.kuro.kujiequ.thread.TowerDataDetailTask;
import cn.tealc.wutheringwavestool.util.LanguageManager;
import de.saxsys.mvvmfx.MvvmFX;
import de.saxsys.mvvmfx.ViewModel;
import javafx.application.Platform;

/**
 * @program: WutheringWavesTool
 * @description:
 * @author: Leck
 * @create: 2024-07-03 18:59
 */
public class MainViewModel implements ViewModel {
    public MainViewModel() {
        checkVersion();
        checkGameLogOpen();
        updateKujiequ();
    }


    public void checkVersion(){
        if (Config.setting.isCheckNewVersion()){
            Platform.runLater(() -> {
                CheckVersionTask task = new CheckVersionTask(true);
                task.setOnSucceeded(workerStateEvent -> {
                    ResponseBody<Release> value = task.getValue();
                    if (value.getCode() == 200){
                        Platform.runLater(()->{
                            MvvmFX.getNotificationCenter().publish(NotificationKey.NOTIFICATION_SHOW_UPDATE,value.getData());
                        });
                    }else if (value.getCode() == -1){
                        MvvmFX.getNotificationCenter().publish(NotificationKey.NOTIFICATION_SHOW_UPDATE,value.getData());
                        MvvmFX.getNotificationCenter().publish(NotificationKey.MESSAGE,new MessageInfo(MessageType.WARNING, LanguageManager.getString("ui.main.message.type01")));
                    }
                });
                Thread.startVirtualThread(task);
            });
        }
    }


    private void checkGameLogOpen(){
        CheckGameConfigTask task = new CheckGameConfigTask();
        task.setOnSucceeded(workerStateEvent -> {
            Boolean value = task.getValue();
            if (!value){ //游戏日志可能被关闭了
                Platform.runLater(()->{
                    NotificationManager.message(MessageInfo.success(LanguageManager.getString("ui.main.sync.message.log.close")));
                });
            }
        });
        Thread.startVirtualThread(task);
    }

    private void updateKujiequ(){
        if (!Config.setting.isNoKuJieQu()){
            //获取深塔刷新时间，同时更新深塔历史记录
            UserInfoDao dao = new UserInfoDao();
            UserInfo main = dao.getMain();
            if (main!=null){
                TowerDataDetailTask task = new TowerDataDetailTask(main);
                task.setOnSucceeded(workerStateEvent -> {
                    ResponseBody<DifficultyTotal> value = task.getValue();
                    if (value.getCode() == 200){
                        long milliseconds = value.getData().getSeasonEndTime();
                        long millisecondsInADay = 24 * 60 * 60 * 1000;
                        double days = (double) milliseconds / (double) millisecondsInADay;
                        if (days > 0 && days < 1){//不足一天时,提醒
                            MvvmFX.getNotificationCenter().publish(NotificationKey.MESSAGE,new MessageInfo(MessageType.WARNING, LanguageManager.getString("ui.main.sync.message.tower")));
                        }
                    }
                });
                Thread.startVirtualThread(task);
            }
        }
    }
}